local function set_up_flee_timer(self, time)
	defense_mob_api.library.set_up_timer(self,
			time,
		function(self, elapsed_after)
			self.fleeing = false
		end,
		"flee_timer")
	self.fleeing = true
end


defense_mob_api.register_mob("mob_paniki:paniki", {
	hp_max = 28,
	collisionbox = {-0.4,-0.4,-0.4, 0.4,0.4,0.4},
	mesh = "mob_paniki_paniki.b3d",
	textures = {"mob_paniki_paniki.png"},
	makes_footstep_sound = false,

	animation = {
		idle = {a=0, b=29, rate=50},
		attack = {a=60, b=89, rate=50},
		move = {a=30, b=59, rate=75},
		move_attack = {a=60, b=89, rate=75},
	},

	mass = 1,
	movement = "air",
	move_speed = 16,
	attack_damage = 1,
	attack_range = 1.1,
	attack_interval = 1.2,

	last_hp = 3,
	flee_timer = 0,
	flee_timer = 0,

	
	
	
	on_step = function(self, dtime)
		if self.fleeing
		then
			local target = self.target
			local pos = self.object:get_pos()
			local delta
			if target.pos
			then
				delta = vector.subtract(pos, target.pos)
			else
				delta = {x = 0, y = 0, z = 0}
			end
			local x = delta.x
			delta.x = delta.x - delta.z * 0.4
			delta.z = delta.z + x * 0.4
			self.destination = vector.add(pos, delta)
			
			--don't stop fleeing shortly after attack
			if self.last_attack_time + 0.25 >= self.timer
			then
				local manip = self.timers["flee_timer"].manipulate_time
				if manip
				then
					manip(self, -dtime)
				end
			end
		else
			self:hunt()
			local hp = self.object:get_hp()
			if hp < self.last_hp
			then
				set_up_flee_timer(self, math.random() / (hp + 1))
			end
			self.last_hp = hp
		end
	end,

	attack = function(self, obj, dir)
		set_up_flee_timer(self, math.random() + 0.5)
	end,
})
minetest.register_alias("paniki", "mob_paniki:paniki")
